package cn.pconline.autoclub.web;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.service.ClubService;
import cn.pconline.autoclub.util.Regex;
import cn.pconline.officialclub.entity.OClub;
import cn.pconline.officialclub.service.OClubService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.gelivable.web.Env;
import org.gelivable.web.EnvUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class UrlInterceptorAdapter implements HandlerInterceptor {

    private static final Logger log = LoggerFactory
            .getLogger(UrlInterceptorAdapter.class);

    public UrlInterceptorAdapter() {
    }

    /**
     * 在业务处理器处理请求之前被调用 如果返回false 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
     *
     * 如果返回true 执行下一个拦截器,直到所有的拦截器都执行完毕 再执行被拦截的Controller 然后进入拦截器链,
     * 从最后一个拦截器往回执行所有的postHandle() 接着再从最后一个拦截器往回执行所有的afterCompletion()
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        String urlDesc = request.getParameter("desc");
        Env env = EnvUtils.getEnv();
        if (request.getRequestURI().matches("^/(wap/)?brand/.*")) {
            String oClubIdStr = request.getParameter("oClubId");
            OClubService oClubService = env.getBean(OClubService.class);
            long oClubId = 0;
            if (urlDesc != null) {
                if (urlDesc.matches(Regex.number.value)) {
                    try {
                        oClubId = Long.parseLong(urlDesc);
                    } catch (NumberFormatException ex) {
                        log.error("transfer desc={} to number error", urlDesc);
                    }
                } else {
                    oClubId = oClubService.findClubIdByUrl(urlDesc);
                }
            } else if (oClubIdStr != null && oClubIdStr.length() > 0 && oClubIdStr.matches(Regex.number.value)) {
                try {
                    oClubId = Long.parseLong(oClubIdStr);
                } catch (NumberFormatException ex) {
                    log.error("transfer oClubId={} to number error", oClubIdStr);
                }
                OClub oClub = oClubService.findValidClub(oClubId);
                if (oClub == null) {
                    urlDesc = null;//URL不存在
                }
            }
            request.setAttribute("myUrl", urlDesc);
            request.setAttribute("oClubId", oClubId);
        } else {
            String clubIdStr = request.getParameter("clubId");
            ClubService clubService = env.getBean(ClubService.class);
            if (urlDesc != null) {
                long clubId = 0;
                if (urlDesc.matches(Regex.nameAdditional.value)) {
                    try {
                        clubId = Long.parseLong(urlDesc);
                    } catch (Exception ex) {
                        log.error("transfer desc={} to number error", urlDesc);
                    }
                } else {
                    clubId = clubService.findClubIdByUrl(urlDesc);
                }
                request.setAttribute("clubId", clubId);
                request.setAttribute("myUrl", urlDesc);
            } else if (clubIdStr != null && clubIdStr.length() > 0 && clubIdStr.matches(Regex.nameAdditional.value)) {
                long clubId = 0;
                try {
                    clubId = Long.parseLong(clubIdStr);
                } catch (Exception ex) {
                    log.error("transfer clubId={} to number error", clubIdStr);
                }
                Club club = clubService.getClub(clubId);
                request.setAttribute("clubId", clubId);
                if (club != null) {
                    request.setAttribute("myUrl", club.getClubUrl());
                }
            }
        }
        return true;
    }

    //在业务处理器处理请求执行完成后,生成视图之前执行的动作 
    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
    }

    /**
     * 在DispatcherServlet完全处理完请求后被调用
     *
     * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
     */
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}
